package com.ant.product.config;

import cn.hutool.core.util.StrUtil;
import lombok.Data;
import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * redisson配置
 */
@Configuration
@ConfigurationProperties("spring.redis")
@Data
public class RedissonConfig {


    private String host;
    private String port;
    private String password;
    private Integer timeout;

    @Bean("redissonClient")
    public RedissonClient redissonDev() {
        Config config = new Config();
        config.useSingleServer()
                .setAddress(host.startsWith("redis://") ? host + ":" + port : "redis://" + host + ":" + port)
                .setPassword(StrUtil.isNotEmpty(password) ? password : null)
                .setTcpNoDelay(true)
                .setKeepAlive(true)
                .setPingConnectionInterval(1000)
                .setConnectTimeout(timeout);
        RedissonClient redisClient = Redisson.create(config);
        return redisClient;
    }
}
